package com.sun.module_home.ui.index

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.carousel.CarouselState
import androidx.compose.material3.carousel.HorizontalUncontainedCarousel
import androidx.compose.material3.carousel.rememberCarouselState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import com.sun.module_home.model.BannerBean

/**
 *@author:sunc
 *@time :2025/8/12 15:09
 *@desc: 描述
 */

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Banner(
    bannerList: MutableList<BannerBean> = mutableListOf()) {
    val pageState: CarouselState = rememberCarouselState(1) { bannerList.size }

    var isAutoScrollEnabled by remember { mutableStateOf(true) }

//    LaunchedEffect(isAutoScrollEnabled) {
//        while (isAutoScrollEnabled) {
//            delay(300)
//            if (isAutoScrollEnabled) {
//
//                pageState.scroll(MutatePriority.UserInput){
//
//                }
//            }
//        }
//    }
    HorizontalUncontainedCarousel(
//    HorizontalMultiBrowseCarousel(
        state = pageState,

        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(vertical = 16.dp, horizontal = 10.dp)
//            .pointerInput(Unit) {
//                detectTapGestures(
//                    onPress = { isAutoScrollEnabled = false },
//                    onTap = { isAutoScrollEnabled = !isAutoScrollEnabled }
//                )
//            }
        ,
//        preferredItemWidth =186.dp,
        itemWidth = 360.dp,
        itemSpacing = 8.dp,
//        contentPadding = PaddingValues(horizontal = 16.dp)
    ) { i ->
        val item = bannerList[i]
        Image(
            modifier = Modifier
                .height(205.dp)
                .maskClip(MaterialTheme.shapes.extraLarge),
            contentDescription = null,
            painter = rememberAsyncImagePainter(item.imagePath),
            contentScale = ContentScale.Crop
        )
    }
}